﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using CRMG.Container.Views.BaseList;
using CRMG.Container.Views.InterViews;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using System;
using System.Collections;
using System.Data;
using System.Resources;
using System.Windows.Forms;

namespace CRMG.Container.Views.Studnt
{
    public partial class FrmAllStudentProcess : FrmBase
    {

        #region [ Private Fields ]

        private ResourceManager formRes, messageRes;

        #region [ Private Manager Classes ]

        private InterViewManager intVwMan;
        private PaymentManager payMan;
        private StudentManager studentMan;
        private TrainManager trainMan;

        #endregion

        private int _studentId = 0;

        private DataTable dtInterViews, dtPayments, dtAvTrains, dtStudent;

        #endregion

        #region [ FrmAllStudentProcess Ctor ]

        public FrmAllStudentProcess(int studentId)
        {
            try
            {
                InitializeComponent();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmAllStudentProcess).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmAllStudentProcess).Assembly);
                _studentId = studentId;
                studentMan = new StudentManager();
                intVwMan = new InterViewManager();
                payMan = new PaymentManager();
                trainMan = new TrainManager();

                pgVwDashboard.Text = formRes.GetString("FrmAllStudentProcess.pgVwDashboard");
                pgVwPayments.Text = formRes.GetString("FrmAllStudentProcess.pgVwPayments");
                pgVwAvTrains.Text = formRes.GetString("FrmAllStudentProcess.pgVwAvTrains");

                lblName.Text = formRes.GetString("FrmAllStudentProcess.lblName");
                lblPhone.Text = formRes.GetString("FrmAllStudentProcess.lblPhone");
                lblMobile.Text = formRes.GetString("FrmAllStudentProcess.lblMobile");
                lblEMail.Text = formRes.GetString("FrmAllStudentProcess.lblEMail");
                lblWorkMail.Text = formRes.GetString("FrmAllStudentProcess.lblWorkMail");

                lblAdres.Text = formRes.GetString("FrmAllStudentProcess.lblAdres");
                lblFirstInterView.Text = formRes.GetString("FrmAllStudentProcess.lblFirstInterView");
                lblLastInterView.Text = formRes.GetString("FrmAllStudentProcess.lblLastInterView");
                lblPhoto.Text = formRes.GetString("FrmAllStudentProcess.lblPhoto");

                lblInterViews.Text = formRes.GetString("FrmAllStudentProcess.lblInterViews");
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmAllStudentProcess.Ctor.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2096,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2096)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmAllStudentProcess.Ctor.Error"),
                    messageRes.GetString("Error"));
            }
        }

        #endregion

        #region [ FormLoad ]

        private void FormLoad(object sender, EventArgs e)
        {
            try
            {
                dtStudent = studentMan.GetDetailsOfStudent(_studentId);
                foreach (DataRow rw in dtStudent.Rows)
                {
                    txtName.Text = rw["FullName"].ToStr();
                    txtTel.Text = rw["Phone"].ToStr();
                    txtMobile.Text = rw["MobilePhone"].ToStr();
                    txtEMail.Text = rw["EMail"].ToStr();
                    txtWorkEMail.Text = rw["WorkEMail"].ToStr();
                    txtAdres.Text = rw["Address"].ToStr();
                    pcbxStudent.Image = rw["Resim"].ToImage();
                }
                dtInterViews = intVwMan.GetInterViewsOfStudent(_studentId);
                for (int interviewCounter = 0; interviewCounter < dtInterViews.Rows.Count; interviewCounter++)
                {
                    dtInterViews.Rows[interviewCounter]["InterViewTime"] =
                        dtInterViews.Rows[interviewCounter]["InterViewTime"].ToStr().ToDateTime().ToString("dd MMMM yyyy dddd HH:mm");
                }
                switch (dtInterViews.Rows.Count)
                {
                    case 0:
                        break;
                    case 1:
                        txtFirstInterView.Text = dtInterViews.Rows[0]["InterViewTime"].ToStr();
                        break;
                    default:
                        txtFirstInterView.Text = dtInterViews.Rows[0]["InterViewTime"].ToStr();
                        txtLastInterView.Text = dtInterViews.Rows[1]["InterViewTime"].ToStr();
                        break;
                }
                txtAllInterViews.ResetText();
                txtAllInterViews.Text = dtInterViews.GetColumnsAsString("UserFullName", "StudentFullName", "InterViewTime", "Remarks", "Comments");
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmAllStudentProcess.Load.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2098,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2098)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmAllStudentProcess.Load.Error"),
                    messageRes.GetString("Error"));
            }
        }

        #endregion


        private void pgVwStudentAllP_PageIndexChanged(object sender, Telerik.WinControls.UI.RadPageViewIndexChangedEventArgs e)
        {
            try
            {
                if (e.Page == pgVwPayments)
                {
                    if (dtPayments == null)
                    {
                        dtPayments = payMan.GetPaymentsOfStudent(_studentId);
                    }
                }
                else if (e.Page == pgVwAvTrains)
                {
                    if (dtAvTrains == null)
                    {
                        dtAvTrains = trainMan.GetAvailableTrainsOfStudent(_studentId);
                    }
                }
                else if (e.Page == pgVwDashboard)
                {
                }
                else { }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmAllStudentProcess.StudentAllPageIndexChanged.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2097,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2097)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmAllStudentProcess.StudentAllPageIndexChanged.Error"),
                    messageRes.GetString("Error"));
            }
        }

        private void InterviewClose(object sender, FormClosedEventArgs e)
        {
            try
            {
                dtInterViews = intVwMan.GetInterViewsOfStudent(_studentId);
                txtAllInterViews.ResetText();
                txtAllInterViews.Text = dtInterViews.GetColumnsAsString("UserFullName", "StudentFullName", "InterViewTime", "Remarks", "Comments");

            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("RefreshRecords.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2103,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2103)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("RefreshRecords.Error"),
                    messageRes.GetString("Error"));
            }
        }

        private void btnAddInterView_Click(object sender, EventArgs e)
        {
            using (FrmInterView frmItrVw = new FrmInterView(_studentId))
            {
                frmItrVw.FormClosed += this.InterviewClose;
                frmItrVw.ShowDialog();
            }
        }

        private const Keys CopyKey = Keys.Control | Keys.C;
        private const Keys PasteKey = Keys.Control | Keys.V;
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((keyData == CopyKey) || (keyData == PasteKey))
            {
                return true;
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }
    }
}
